package com.dtagri.fjwlw.ui;

import java.util.List;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.TextView;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.bean.SmallArea;
import com.dtagri.fjwlw.bean.TjAreaMsg;
import com.dtagri.fjwlw.bean.TjEquipment;
import com.dtagri.fjwlw.bean.UserOperate;
import com.dtagri.fjwlw.widget.CommonDialog;
import com.dtagri.fjwlw.widget.DialogHelper;

/**
 * 历史数据过滤对话框
 * 
 * @author wxjn
 * 
 */
public class ZFilterDialog extends CommonDialog implements OnClickListener {

	private TextView bigArea;
	private TextView smallArea;
	private TextView equipment;
	private Context context;

	public UserOperate getUserOperate() {
		return userOperate;
	}

	private UserOperate userOperate;

	public int getToYear() {
		return toYear;
	}

	public int getToMonth() {
		return toMonth;
	}

	public int getToDay() {
		return toDay;
	}

	public int getFromYear() {
		return fromYear;
	}

	public int getFromMonth() {
		return fromMonth;
	}

	public int getFromDay() {
		return fromDay;
	}

	private int toYear;
	private int toMonth;
	private int toDay;
	private int fromYear;
	private int fromMonth;
	private int fromDay;
	private TextView timeFrom;
	private TextView timeTo;

	public ZFilterDialog(Context context) {
		this(context, R.style.dialog_common);
	}

	public ZFilterDialog(Context context, int defStyle) {
		super(context, defStyle);
		this.context = context;
		userOperate = AppContext.userOperate;

		fromDay = ((HistoricalDataActivity) context).getFromDay();
		fromMonth = ((HistoricalDataActivity) context).getFromMonth();
		fromYear = ((HistoricalDataActivity) context).getFromYear();
		toDay = ((HistoricalDataActivity) context).getToDay();
		toMonth = ((HistoricalDataActivity) context).getToMonth();
		toYear = ((HistoricalDataActivity) context).getToYear();

		initViews();
		setTitle("筛选");
		setNegativeButton("取消", null);
	}

	private void initViews() {
		View view = getLayoutInflater().inflate(R.layout.dialog_report, null);
		bigArea = (TextView) view.findViewById(R.id.tv_big_area);
		smallArea = (TextView) view.findViewById(R.id.tv_small_area);
		equipment = (TextView) view.findViewById(R.id.tv_equipment);
		timeFrom = (TextView) view.findViewById(R.id.tv_time_from);
		timeTo = (TextView) view.findViewById(R.id.tv_time_to);

		bigArea.setOnClickListener(this);
		smallArea.setOnClickListener(this);
		equipment.setOnClickListener(this);
		timeFrom.setOnClickListener(this);
		timeTo.setOnClickListener(this);
		super.setContent(view, 0);

		resetTextView();
	}

	/**
	 * 重置三个textView
	 * 
	 * @param tjEquipment
	 * @param curSmallArea
	 * @param tjAreaMsg
	 */
	private void resetTextView() {
		TjEquipment tjEquipment = curEquipment();
		SmallArea curSmallArea = curSmallArea();
		TjAreaMsg tjAreaMsg = curBigArea();
		smallArea.setText(curSmallArea.getSmallName());
		equipment.setText(tjEquipment.getEquipName());
		bigArea.setText(tjAreaMsg.getAreaName());

		String from = fromYear + "-" + (fromMonth + 1) + "-" + fromDay;
		String to = toYear + "-" + (toMonth + 1) + "-" + toDay;

		timeFrom.setText(from);
		timeTo.setText(to);
	}

	protected ZFilterDialog(Context context, boolean flag,
			OnCancelListener listener) {
		super(context, flag, listener);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_big_area:
			show1FilterDialog();
			break;
		case R.id.tv_small_area:
			show2FilterDialog();
			break;
		case R.id.tv_equipment:
			show3FilterDialog();
			break;
		case R.id.tv_time_from:
			showFromTimeDialog();
			break;
		case R.id.tv_time_to:
			showToTimeDialog();
			break;
		}
	}

	/**
	 * 显示二级第1个弹框
	 * 
	 * @param arg0
	 */
	private void show1FilterDialog() {
		final CommonDialog dialog = DialogHelper
				.getPinterestDialogCancelable(context);
		dialog.setTitle("选择生产区");
		dialog.setItems(convertAreaNames(), userOperate.getSpinnerSelected0(),
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						userOperate.setSpinnerSelected0(arg2);
						userOperate.setSpinnerSelected1(0);
						userOperate.setSpinnerSelected2(0);

						resetTextView();
						dialog.dismiss();
					}
				});
		dialog.show();
	}

	/**
	 * 显示二级第2个弹框
	 * 
	 * @param arg0
	 */
	private void show2FilterDialog() {
		final CommonDialog dialog = DialogHelper
				.getPinterestDialogCancelable(context);
		dialog.setTitle("小区");
		dialog.setItems(convertSmallNames(), userOperate.getSpinnerSelected1(),
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						userOperate.setSpinnerSelected1(arg2);
						userOperate.setSpinnerSelected2(0);

						resetTextView();
						dialog.dismiss();
					}
				});
		dialog.show();
	}

	/**
	 * 显示二级第3个弹框
	 * 
	 * @param arg0
	 */
	private void show3FilterDialog() {
		final CommonDialog dialog = DialogHelper
				.getPinterestDialogCancelable(context);
		dialog.setTitle("选择传感器");
		dialog.setItems(convertEquipNames(), userOperate.getSpinnerSelected2(),
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						userOperate.setSpinnerSelected2(arg2);

						resetTextView();
						dialog.dismiss();
					}
				});
		dialog.show();
	}

	/**
	 * 开始时间对话框
	 */
	private void showFromTimeDialog() {

		final DatePickerDialog dateDialog = new DatePickerDialog(context,
				new DatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
					}

				}, fromYear, fromMonth, fromDay);
		DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_NEGATIVE:
					break;
				case DialogInterface.BUTTON_POSITIVE:
					fromYear = dateDialog.getDatePicker().getYear();
					fromMonth = dateDialog.getDatePicker().getMonth();
					fromDay = dateDialog.getDatePicker().getDayOfMonth();
					resetTextView();
					break;
				}
			}
		};
		dateDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				clickListener);
		dateDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认",
				clickListener);
		dateDialog.show();
	}

	/**
	 * 结束时间对话框
	 */
	private void showToTimeDialog() {

		final DatePickerDialog dateDialog = new DatePickerDialog(context,
				new DatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
					}

				}, toYear, toMonth, toDay);
		DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_NEGATIVE:
					break;
				case DialogInterface.BUTTON_POSITIVE:
					toYear = dateDialog.getDatePicker().getYear();
					toMonth = dateDialog.getDatePicker().getMonth();
					toDay = dateDialog.getDatePicker().getDayOfMonth();
					resetTextView();
					break;
				}
			}
		};
		dateDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				clickListener);
		dateDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认",
				clickListener);
		dateDialog.show();
	}

	/**
	 * 获得大区数组
	 * 
	 * @return
	 */
	private String[] convertAreaNames() {
		List<TjAreaMsg> tjAreaMsgs = AppContext.tjAreaMsgs;
		String[] ss = new String[tjAreaMsgs.size()];
		for (int i = 0; i < tjAreaMsgs.size(); i++) {
			TjAreaMsg tjAreaMsg = tjAreaMsgs.get(i);
			ss[i] = tjAreaMsg.getAreaName();
		}
		return ss;
	}

	/**
	 * 获得小区数组
	 * 
	 * @return
	 */
	private String[] convertSmallNames() {
		List<SmallArea> smallAreas = curBigArea().getSmallAreas();
		String[] ss = new String[smallAreas.size()];
		int i = 0;
		for (SmallArea smallArea : smallAreas) {
			ss[i] = smallArea.getSmallName();
			i++;
		}
		return ss;
	}

	/**
	 * 获得传感器数组
	 * 
	 * @return
	 */
	private String[] convertEquipNames() {
		List<TjEquipment> tjEquipments = curSmallArea().getTjEquipments();
		String[] ss = new String[tjEquipments.size()];
		int i = 0;
		for (TjEquipment tjEquipment : tjEquipments) {
			ss[i] = tjEquipment.getEquipName();
			i++;
		}
		return ss;
	}

	/**
	 * 获得当前的传感器
	 * 
	 * @param spinnerSelected2
	 * @param spinnerSelected1
	 * @param spinnerSelected0
	 * 
	 * @return
	 */
	public TjEquipment curEquipment() {
		SmallArea res = curSmallArea();
		List<TjEquipment> tjEquipments = res.getTjEquipments();
		if (tjEquipments.size() == 0) {
			return null;
		}
		TjEquipment tjEquipment = tjEquipments.get(userOperate
				.getSpinnerSelected2());
		return tjEquipment;
	}

	/**
	 * 获得当前小区
	 * 
	 * @param spinnerSelected1
	 * @param spinnerSelected0
	 * 
	 * @return
	 */
	public SmallArea curSmallArea() {
		List<SmallArea> tjAreaMsg = curBigArea().getSmallAreas();
		int i = 0;
		SmallArea res = null;
		for (SmallArea smallArea : tjAreaMsg) {
			if (userOperate.getSpinnerSelected1() == i) {
				res = smallArea;
			}
			i++;
		}
		return res;
	}

	public TjAreaMsg curBigArea() {
		TjAreaMsg tjAreaMsg = AppContext.tjAreaMsgs.get(userOperate
				.getSpinnerSelected0());
		return tjAreaMsg;
	}
}
